package com.example.service;

import com.example.entity.SysStudent;
import com.example.exception.CustomException;
import com.example.mapper.SysStudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysStudentService {

    @Resource
    private SysStudentMapper sysStudentMapper;

    public List<SysStudent> selectAll(SysStudent sysStudent) {
        return sysStudentMapper.selectAll(sysStudent);
    }

    //分页查询
    public PageInfo<SysStudent> selectPage(SysStudent sysStudent, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysStudent> list = sysStudentMapper.selectAll(sysStudent);
        return PageInfo.of(list);
    }

    public void add(SysStudent sysStudent) {
        sysStudentMapper.insert(sysStudent);
    }

    public void update(SysStudent sysStudent) {
        sysStudentMapper.update(sysStudent);
    }

    public void delete(Integer id) {
        sysStudentMapper.delete(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            sysStudentMapper.delete(id);
        }
    }


    public SysStudent login(SysStudent sysStudent) {
        String studentName = sysStudent.getStudentName();
        SysStudent dbstudent = sysStudentMapper.selectByName(studentName);
        if(dbstudent == null) {
            throw new CustomException("500","名字错误");
        }
        return dbstudent;
    }
}
